package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.SpecialistOnlineListViewAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.tools.CheckNetWork;

/**
 * 专家在线界面
 * 
 * @author lkx Created at 2015-10-14 上午9:39:09
 * @version 1.0
 */
public class SpecialistOnlineActivity extends BaseActivity {
	private ActivityTitle title;
	TextView emptyView;
	PullToRefreshListView mRefreshListView;
	SpecialistOnlineListViewAdapter adapter;
	private JSONArray array;

	// private ListView sortListView;
	// private SideBar sideBar;
	// private TextView dialog;
	// private SortAdapter adapter;
	//
	// /**
	// * 汉字转换成拼音的类
	// */
	// private PinyinKit characterParser;
	// private List<SortModel> SourceDateList;
	//
	// /**
	// * 根据拼音来排列ListView里面的数据类
	// */
	// private PinyinComparator pinyinComparator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_specialist_online);
		initView();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.SpecialistOnlineActivityTitle);
		title.setTitle("专家在线");
		title.setViewVersible(title.titleBack);
		title.setViewGone(title.titleMsg, title.titleRefresh, title.rightLayout);
		emptyView = (TextView) findViewById(R.id.emptyView);

		// 默认显示为空
		emptyView.setText("正在加载");
		emptyView.setVisibility(View.VISIBLE);
		mRefreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefresh);
		mRefreshListView.setVisibility(View.GONE);
		adapter = new SpecialistOnlineListViewAdapter(this);
		mRefreshListView.setAdapter(adapter);
		mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				if (CheckNetWork.isConnected(SpecialistOnlineActivity.this)) {
					loadData();
				} else {
					ToastKit.showShort(SpecialistOnlineActivity.this, "请连接网络");
				}
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				if (CheckNetWork.isConnected(SpecialistOnlineActivity.this)) {
					loadData();
				} else {
					ToastKit.showShort(SpecialistOnlineActivity.this, "请连接网络");
				}
			}

		});

		loadData();

		// // 实例化汉字转拼音类
		// characterParser = PinyinKit.getInstance();
		//
		// pinyinComparator = new PinyinComparator();
		//
		// sideBar = (SideBar) findViewById(R.id.sidrbar);
		// dialog = (TextView) findViewById(R.id.dialog);
		// sideBar.setTextView(dialog);
		//
		// // 设置右侧触摸监听
		// sideBar.setOnTouchingLetterChangedListener(new
		// OnTouchingLetterChangedListener() {
		//
		// @Override
		// public void onTouchingLetterChanged(String s) {
		// // 该字母首次出现的位置
		// int position = adapter.getPositionForSection(s.charAt(0));
		// if (position != -1) {
		// sortListView.setSelection(position);
		// }
		//
		// }
		// });
		//
		// sortListView = (ListView) findViewById(R.id.country_lvcountry);
		// sortListView.setOnItemClickListener(new OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> parent, View view, int
		// position, long id) {
		// // 这里要利用adapter.getItem(position)来获取当前position所对应的对象
		// Toast.makeText(getApplication(), ((SortModel)
		// adapter.getItem(position)).getName(), Toast.LENGTH_SHORT).show();
		// }
		// });
		//
		// // SourceDateList =
		// // filledData(getResources().getStringArray(R.array.date));
		// SourceDateList = filledData();
		// // 根据a-z进行排序源数据
		// Collections.sort(SourceDateList, pinyinComparator);
		// adapter = new SortAdapter(this, SourceDateList);
		// sortListView.setAdapter(adapter);
		mRefreshListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// if (array != null || array.length() != 0) {
				// String id = null;
				// try {
				// id = array.getJSONObject(arg2 - 1).getString("uid");
				// } catch (JSONException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				// Intent intent = new Intent(SpecialistOnlineActivity.this,
				// UserDetailActivity.class);
				// intent.putExtra("userId", id);
				// startActivity(intent);
				// }

			}
		});
	}

	/**
	 * 加载数据
	 * 
	 * @author Vity Created at 2015-10-22 上午12:09:45
	 */
	public void loadData() {

		Api.get("specialist/recommend", ApiParam.Empty, new ApiCallback(this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getDatas() == null || result.getDatas().length() == 0) {
					emptyView.setText("没有专家在线");
					emptyView.setVisibility(View.VISIBLE);
					mRefreshListView.setVisibility(View.GONE);
				} else {
					array = result.getDatas();
					emptyView.setVisibility(View.GONE);
					mRefreshListView.setVisibility(View.VISIBLE);
					adapter.setDatas(result.getDatas());
				}
				mRefreshListView.onRefreshComplete();
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				emptyView.setText("没有专家在线");
				emptyView.setVisibility(View.VISIBLE);
				mRefreshListView.setVisibility(View.GONE);
			}
		}, this);

	}

	// /**
	// * 为ListView填充数据
	// *
	// * @param
	// * @return
	// */
	// private List<SortModel> filledData() {
	// final List<SortModel> mSortList = new ArrayList<SortModel>();
	// Api.get("specialist/recommend", ApiParam.create(), new ApiCallback(this)
	// {
	//
	// @Override
	// public void handleSuccess(ApiResult result) {
	// if (result.getCode() == 100) {
	// if (result.getDatas() != null && result.getDatas().length() > 0) {
	//
	// JSONArray jsonarray = result.getDatas();
	// for (int i = 0; i < jsonarray.length(); i++) {
	// SortModel sortModel = new SortModel();
	// try {
	// sortModel.setName(jsonarray.getJSONObject(i).getString("title"));
	// sortModel.setImageurl(jsonarray.getJSONObject(i).getString("image"));
	// sortModel.setSortLetters(jsonarray.getJSONObject(i).getString("fcahr"));
	// sortModel.setUid(jsonarray.getJSONObject(i).getString("uid"));
	// sortModel.setMid(jsonarray.getJSONObject(i).getString("mid"));
	// sortModel.setRid(jsonarray.getJSONObject(i).getString("rid"));
	// sortModel.setRank(jsonarray.getJSONObject(i).getString("rank"));
	// sortModel.setRankImage(jsonarray.getJSONObject(i).getString("rankImage"));
	// } catch (JSONException e) {
	// e.printStackTrace();
	// }
	// mSortList.add(sortModel);
	//
	// // // 汉字转换成拼音
	// // String pinyin =
	// // PinyinKit.getSelling(jsonarray.getJSONObject(i).getString("title"));
	// // String sortString = pinyin.substring(0,
	// // 1).toUpperCase();
	// //
	// // // 正则表达式，判断首字母是否是英文字母
	// // if (sortString.matches("[A-Z]")) {
	// // sortModel.setSortLetters(sortString.toUpperCase());
	// // } else {
	// // sortModel.setSortLetters("#");
	// // }
	//
	// }
	// emptyView.setVisibility(View.GONE);
	// findViewById(R.id.rl_listview).setVisibility(View.VISIBLE);
	// } else {
	// emptyView.setText("当前没有专家在线");
	// emptyView.setVisibility(View.VISIBLE);
	// findViewById(R.id.rl_listview).setVisibility(View.GONE);
	// }
	//
	// } else {
	// emptyView.setText("加载失败");
	// emptyView.setVisibility(View.VISIBLE);
	// findViewById(R.id.rl_listview).setVisibility(View.GONE);
	// }
	//
	// }
	//
	// @Override
	// public void handleFailure(Exception e, String msg) {
	// emptyView.setText("加载失败");
	// emptyView.setVisibility(View.VISIBLE);
	// findViewById(R.id.rl_listview).setVisibility(View.GONE);
	// Log.e("SPonlineAct", msg);
	//
	// }
	// }, this);
	//
	// /*
	// * List<SortModel> mSortList = new ArrayList<SortModel>();
	// *
	// * for (int i = 0; i < date.length; i++) { SortModel sortModel = new
	// * SortModel(); sortModel.setName(date[i]); // 汉字转换成拼音 String pinyin =
	// * PinyinKit.getSelling(date[i]); String sortString =
	// * pinyin.substring(0, 1).toUpperCase();
	// *
	// * // 正则表达式，判断首字母是否是英文字母 if (sortString.matches("[A-Z]")) {
	// * sortModel.setSortLetters(sortString.toUpperCase()); } else {
	// * sortModel.setSortLetters("#"); }
	// *
	// * mSortList.add(sortModel); }
	// */
	// return mSortList;
	//
	// }
}
